#!/usr/bin/python
# encoding: utf-8
import math


class ModelForEndStageLiverDiseaseScore():
    """
    终末期肝病MELD评分
    1. Kamath PS,Wiesner RH,Malinchoc M,et al. A model to predict survival in patients with end-stage liver disease Hepatology.2001;33(2):464-70.
    2. Kamath PS, Kim WR. "The model for end-stage liver disease (MELD)". Hepatology .2007;45 (3): 797–805.
    3. http://en.wikipedia.org/wiki/Model_for_End-Stage_Liver_Disease
    4. http://doctor-network.com/public/littletools/262.html
    """

    def __init__(self):
        """
        Etiology of end stage liver disease 终末期肝病病因 MELD=3.78*Ln（总胆红素）+11.2*Ln（INR）+9.57*Ln（血肌酐）+6.43*（胆汁性或酒精性0，其他1）
        INR中文称为国bai际标准化比值,是从凝du血酶原时间(PT)和测定试zhi剂的国际敏感指数(ISI)推算出来的dao,INR=(病人PT/正常对照PT)ISI,采用INR使不同实验室和不同试剂测定的PT具有可比性,便于统一 用药标准。
        # 血肌酐Serum creatinine（mmol/L）
        # total bilirubin total bilirubin
        """

    def calc(self, data):
        """
        终末期肝病MELD评分
        """
        if int(data['age']) <= 12:
            return {"error": "请使用PELD评分"}
        else:
            TB = float(data['TB'])
            if TB < 1:
                TB = 1
            SC = float(data['SC'])
            if SC < 1:
                SC = 1
            INR = float(data[
                            'INR'])
            if data['DTC'] == "1":
                SC = 4
            if data['ENESLD'] == "0":
                MELD = int(3.78 * math.log(TB, math.e) + 11.2 * math.log(INR, math.e) + 9.57 * math.log(SC, math.e))
            else:
                MELD = int(
                    3.78 * math.log(TB, math.e) + 11.2 * math.log(INR, math.e) + 9.57 * math.log(SC, math.e) + 6.43)
            if MELD > 18:
                status = "终末期肝病高危"
            elif 15 <= MELD <= 18:
                status = "终末期肝病中危"
            else:
                status = "终末期肝病低危"
            if MELD >= 40:
                death = "终末期肝病死率可能达71.3%"
            elif 30 <= MELD <= 39:
                death = "终末期肝病死率可能达52.6%"
            elif 20 <= MELD <= 29:
                death = "终末期肝病死率可能达19.6%"
            elif 10 <= MELD <= 19:
                death = "终末期肝病死率可能达6%"
            else:
                death = "终末期肝病死率可能达1.9%"
            return {"MELD": MELD, "终末期肝病死率": death, "终末期肝病危险等级": status}
